from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:fuckoff@192.168.2.50:3306/mytest"
db = SQLAlchemy(app)


groups = db.Table(
    "t_groups",
    db.Column("group_id", db.Integer, db.ForeignKey("t_cmdb_group.id")),
    db.Column("user_id", db.Integer, db.ForeignKey("t_cmdb_user.id"))
)

machines = db.Table(
    "t_machines",
    db.Column("ecs_id", db.Integer, db.ForeignKey("t_cmdb_ecs.id")),
    db.Column("project_id", db.Integer, db.ForeignKey("t_cmdb_project.id"))
)


class Group(db.Model):

    __tablename__ = "t_cmdb_group"
    __table_args__ = {"mysql_collate": "utf8_general_ci"}

    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(32), unique=True, nullable=False)


class ECS(db.Model):

    __tablename__ = "t_cmdb_ecs"
    __table_args__ = {"mysql_collate": "utf8_general_ci"}

    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(128), nullable=False)
    internal_addr = db.Column(db.String(32), nullable=False)
    external_addr = db.Column(db.String(32), nullable=True)


class RDS(db.Model):

    __tablename__ = "t_cmdb_rds"
    __table_args__ = {"mysql_collate": "utf8_general_ci"}

    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(64), nullable=False)
    belong_to = db.Column(db.String(16), nullable=False)
    internal_addr = db.Column(db.String(128), unique=True, nullable=False)
    external_addr = db.Column(db.String(128), unique=True, nullable=False)
    zone = db.Column(db.String(64), nullable=False)
    cpu = db.Column(db.String(4), nullable=False)
    mem = db.Column(db.String(8), nullable=False)
    disk_space = db.Column(db.String(8), nullable=True)
    max_conn = db.Column(db.Integer, nullable=True)
    max_iops = db.Column(db.Integer, nullable=True)


class User(db.Model):

    __tablename__ = "t_cmdb_user"
    __table_args__ = {"mysql_collate": "utf8_general_ci"}

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(32), unique=True, nullable=False)
    mobile = db.Column(db.String(11), nullable=True)
    email = db.Column(db.String(64), unique=True, nullable=True)

    group_id = db.Column(db.Integer, db.ForeignKey("t_cmdb_group.id"))
    group = db.relationship("Group", backref=db.backref("users", lazy=True))


class Project(db.Model):

    __tablename__ = "t_cmdb_project"
    __table_args__ = {"mysql_collate": "utf8_general_ci"}

    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(128), nullable=False)
    name = db.Column(db.String(32), unique=True, nullable=False)
    database = db.Column(db.String(32), unique=True, nullable=False)
    port = db.Column(db.String(5), nullable=False)

    users = db.relationship("User", secondary=groups,
                            backref=db.backref("projects", lazy=True))
    machines = db.relationship("ECS", secondary=machines,
                               backref=db.backref("projects", lazy=True))
